package com.feng;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * @description: Spring 通过任务执行器(TaskExecutor)来实现多线程和并发编程。
 * EnableAsync 开启对异步的支持
 * @author: lrf
 * @create: 2019-09-07
 **/
@Configuration
@ComponentScan
@EnableAsync
public class TaskExecutorConfig implements AsyncConfigurer {

    /**
     * 获取一个基于线程池的 TaskExecutor
     * 获取异步执行器
     * @return {@link ThreadPoolTaskExecutor}
     */
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();

        taskExecutor.setCorePoolSize(5);

        taskExecutor.setMaxPoolSize(10);

        taskExecutor.setQueueCapacity(25);

        taskExecutor.initialize();

        return taskExecutor;
    }


    /**
     * 获取异步未捕获的执行器处理器
     * @return {@link AsyncUncaughtExceptionHandler}
     */
    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
}
